package com.yng.simbot01.service;

import com.yng.simbot01.Config.KetWordsReply;
import com.yng.simbot01.Config.pojo.Keywords;
import love.forte.simbot.event.ChatGroupMessageEvent;
import love.forte.simbot.event.MessageEvent;
import love.forte.simbot.message.MessageContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.concurrent.CompletableFuture;

@Component
public class KeyWordService {
    @Autowired
    private KetWordsReply ketWordsReply;

    public CompletableFuture<?> KeyWordFilter(MessageEvent event){
        MessageContent messageContent = event.getMessageContent();
        String plainText = messageContent.getPlainText();

        String result = keywordProcessing(plainText);
        //不为Null就发送给用户
        if(result != null) return event.replyAsync(result);

        //说明没有匹配的关键字
        return null;
    }


    /**
     *
     * @param userMessage 用户发来的消息
     * @return 为null表示没有关键字需要过滤
     */
    private String keywordProcessing(String userMessage){
        List<Keywords> keyWords = ketWordsReply.getKeyWords();

        List<Keywords> result
                = keyWords.stream()
                .filter(keywords -> userMessage.toLowerCase().contains(keywords.getUserMessage().toLowerCase()))
                .toList();

        if(CollectionUtils.isEmpty(result)){
            return null;
        }

        return result.get(0).getReply();
    }
}
